/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "PriceIntervalDialog.h"
#include "StockDatabase.h"
#include "Utilities.h"

namespace QSThink
{
	PriceIntervalDialog::PriceIntervalDialog(QWidget *parent)
		: QDialog(parent),_ui(new Ui::PriceIntervalUI)
	{
		_ui->setupUi(this);

		_ui->dateEditStart->setDate(QDate::currentDate());
		_ui->dateEditEnd->setDate(QDate::currentDate());

		//void	dateChanged ( const QDate & date )
		connect(_ui->dateEditStart,SIGNAL(dateChanged( const QDate&)),this,SLOT(OnStartDateChanged(const QDate&)));

		connect(_ui->dateEditEnd,SIGNAL(dateChanged( const QDate&)),this,SLOT(OnEndDateChanged(const QDate&)));


	}

	PriceIntervalDialog::~PriceIntervalDialog()
	{
		delete _ui;
	}

	void PriceIntervalDialog::load( QString symbol )
	{
		_symbol=symbol;

		QDate startDate=_ui->dateEditStart->date();
		QDate endDate=_ui->dateEditEnd->date();


		double highestPrice=StockDatabase::getSingleton().getHighestPriceEx(_symbol,startDate,endDate);
		double lowestPrice=StockDatabase::getSingleton().getLowestPriceEx(_symbol,startDate,endDate);

		highestPrice/=100.0;
		lowestPrice/=100.0;

		_ui->labelHighestPrice->setText(QString::number(highestPrice));
		_ui->labelLowestPrice->setText(QString::number(lowestPrice));

	}

	void PriceIntervalDialog::OnStartDateChanged( const QDate& date )
	{
		QDate startDate=_ui->dateEditStart->date();
		QDate endDate=_ui->dateEditEnd->date();
		double highestPrice=StockDatabase::getSingleton().getHighestPriceEx(_symbol,startDate,endDate);
		double lowestPrice=StockDatabase::getSingleton().getLowestPriceEx(_symbol,startDate,endDate);


		highestPrice/=100.0;
		lowestPrice/=100.0;

		_ui->labelHighestPrice->setText(QString::number(highestPrice));
		_ui->labelLowestPrice->setText(QString::number(lowestPrice));
	}

	void PriceIntervalDialog::OnEndDateChanged( const QDate& date )
	{
		QDate startDate=_ui->dateEditStart->date();
		QDate endDate=_ui->dateEditEnd->date();
		double highestPrice=StockDatabase::getSingleton().getHighestPriceEx(_symbol,startDate,endDate);
		double lowestPrice=StockDatabase::getSingleton().getLowestPriceEx(_symbol,startDate,endDate);
		highestPrice/=100.0;
		lowestPrice/=100.0;




		_ui->labelHighestPrice->setText(QString::number(highestPrice));
		_ui->labelLowestPrice->setText(QString::number(lowestPrice));

	}

	
}
